﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace LevenBlog.Core.Utils
{
    public sealed class MailSender
    {
        public string FromMail { get; set; }
        public string SmtpHost { get; set; }
        public string SmtpUser { get; set; }
        public string SmtpPassword { get; set; }
        public MailAddressCollection ToMails { get; private set; }
        public MailEntity Mail { get; set; }

        private SmtpClient smtpClient;

        public MailSender()
        {
            ToMails = new MailAddressCollection();
        }

        public MailSender(string host)
        {
            SmtpHost = host;
            ToMails = new MailAddressCollection();
        }

        public void Send()
        {
            NetworkCredential nc = new NetworkCredential();
            nc.UserName = SmtpUser;
            nc.Password = SmtpPassword;
            smtpClient = new SmtpClient(SmtpHost);
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(FromMail);
            foreach (MailAddress item in ToMails)
            {
                mailMessage.To.Add(item);
            }
            mailMessage.Subject = Mail.Title;
            mailMessage.Body = Mail.Content;
            mailMessage.IsBodyHtml = true;
            mailMessage.SubjectEncoding = Encoding.Default;
            mailMessage.BodyEncoding = Encoding.Default;
            smtpClient.Credentials = nc;
            //smtpClient.Send(mailMessage);
            smtpClient.SendAsync(mailMessage, null);
        }
    }
}
